{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "titanic_survival = pd.read_csv('titanic_train.csv')\n",
    "titanic_survival.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "177\n"
     ]
    }
   ],
   "source": [
    "age = titanic_survival['Age']\n",
    "# print(age.head())\n",
    "age_is_null = pd.isnull(age)# Age为空的布尔值，空为True\n",
    "# print(age_is_null)\n",
    "age_null_true = age[age_is_null]\n",
    "age_null_count = len(age_null_true)# Age为空的个数\n",
    "print(age_null_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29.6991176471\n"
     ]
    }
   ],
   "source": [
    "good_ages = titanic_survival['Age'][age_is_null==False]# 去掉缺失值\n",
    "# print(good_ages)\n",
    "# mean_age = sum(good_ages) / len(good_ages)# 求平均值\n",
    "mean_age = titanic_survival['Age'].mean()# 求平均值\n",
    "print(mean_age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        Survived\n",
      "Pclass          \n",
      "1       0.629630\n",
      "2       0.472826\n",
      "3       0.242363\n"
     ]
    }
   ],
   "source": [
    "passanger_survival = titanic_survival.pivot_table(index='Pclass', values='Survived', aggfunc=np.mean)# 根据平均值统计各等级舱位的获救人数\n",
    "print(passanger_survival)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Age\n",
      "Pclass           \n",
      "1       38.233441\n",
      "2       29.877630\n",
      "3       25.140620\n"
     ]
    }
   ],
   "source": [
    "passanger_age = titanic_survival.pivot_table(index='Pclass', values='Age')# 统计各仓位年龄\n",
    "print(passanger_age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                Fare  Survived\n",
      "Embarked                      \n",
      "C         10072.2962        93\n",
      "Q          1022.2543        30\n",
      "S         17439.3988       217\n"
     ]
    }
   ],
   "source": [
    "port_stats = titanic_survival.pivot_table(index='Embarked', values=['Fare', 'Survived'], aggfunc=np.sum)# 查看两个数据间的关系\n",
    "print(port_stats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# drop_na_columns = titanic_survival.dropna(axis=1)# 去掉含有缺失值数据\n",
    "# new_titanic_survival = titanic_survival.dropna(axis=0, subset=['Age', 'Sex'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28.0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# row_index_83_age = titanic_survival['Age'].loc[83]\n",
    "row_index_83_age = titanic_survival.loc[83, 'Age']\n",
    "row_index_766_pclass = titanic_survival.loc[766, 'Pclass']\n",
    "print(row_index_83_age)\n",
    "print(row_index_766_pclass)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   PassengerId  Survived  Pclass                                  Name   Sex  \\\n",
      "0          631         1       1  Barkworth, Mr. Algernon Henry Wilson  male   \n",
      "1          852         0       3                   Svensson, Mr. Johan  male   \n",
      "2          494         0       1               Artagaveytia, Mr. Ramon  male   \n",
      "3           97         0       1             Goldschmidt, Mr. George B  male   \n",
      "4          117         0       3                  Connors, Mr. Patrick  male   \n",
      "5          673         0       2           Mitchell, Mr. Henry Michael  male   \n",
      "6          746         0       1          Crosby, Capt. Edward Gifford  male   \n",
      "7           34         0       2                 Wheadon, Mr. Edward H  male   \n",
      "8           55         0       1        Ostby, Mr. Engelhart Cornelius  male   \n",
      "9          281         0       3                      Duane, Mr. Frank  male   \n",
      "\n",
      "    Age  SibSp  Parch      Ticket     Fare Cabin Embarked  \n",
      "0  80.0      0      0       27042  30.0000   A23        S  \n",
      "1  74.0      0      0      347060   7.7750   NaN        S  \n",
      "2  71.0      0      0    PC 17609  49.5042   NaN        C  \n",
      "3  71.0      0      0    PC 17754  34.6542    A5        C  \n",
      "4  70.5      0      0      370369   7.7500   NaN        Q  \n",
      "5  70.0      0      0  C.A. 24580  10.5000   NaN        S  \n",
      "6  70.0      1      1   WE/P 5735  71.0000   B22        S  \n",
      "7  66.0      0      0  C.A. 24579  10.5000   NaN        S  \n",
      "8  65.0      0      1      113509  61.9792   B30        C  \n",
      "9  65.0      0      0      336439   7.7500   NaN        Q  \n"
     ]
    }
   ],
   "source": [
    "new_titanix_survival = titanic_survival.sort_values('Age', ascending=False)\n",
    "titanic_reindex = new_titanix_survival.reset_index(drop=True)# 重新设置索引\n",
    "print(titanic_reindex.head(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "0\n",
      "2\n",
      "Kantor, Mr. Sinai\n",
      "male\n",
      "34.0\n",
      "1\n",
      "0\n",
      "244367\n",
      "26.0\n",
      "nan\n",
      "S\n",
      "PassengerId                  100\n",
      "Survived                       0\n",
      "Pclass                         2\n",
      "Name           Kantor, Mr. Sinai\n",
      "Sex                         male\n",
      "Age                           34\n",
      "SibSp                          1\n",
      "Parch                          0\n",
      "Ticket                    244367\n",
      "Fare                          26\n",
      "Cabin                        NaN\n",
      "Embarked                       S\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 自定义函数\n",
    "def hundredth_row(column):\n",
    "    hundredth_item = column.loc[99]\n",
    "    return hundredth_item\n",
    "\n",
    "hundredth_row = titanic_survival.apply(hundredth_row)\n",
    "print(hundredth_row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PassengerId      0\n",
      "Survived         0\n",
      "Pclass           0\n",
      "Name             0\n",
      "Sex              0\n",
      "Age            177\n",
      "SibSp            0\n",
      "Parch            0\n",
      "Ticket           0\n",
      "Fare             0\n",
      "Cabin          687\n",
      "Embarked         2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "def not_null_count(column):\n",
    "    column_null = pd.isnull(column)\n",
    "    null = column[column_null]\n",
    "    return len(null)\n",
    "\n",
    "column_null_count = titanic_survival.apply(not_null_count)\n",
    "print(column_null_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    Third Class\n",
      "1    First Class\n",
      "2    Third Class\n",
      "3    First Class\n",
      "4    Third Class\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "def which_class(row):\n",
    "    pclass = row['Pclass']\n",
    "    if pd.isnull(pclass):\n",
    "        return 'Unkown'\n",
    "    elif pclass==1:\n",
    "        return 'First Class'\n",
    "    elif pclass==2:\n",
    "        return 'Second Class'\n",
    "    elif pclass==3:\n",
    "        return 'Third Class'\n",
    "    \n",
    "classes = titanic_survival.apply(which_class, axis=1)\n",
    "print(classes.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "def is_minor(row):\n",
    "    age = row['Age']\n",
    "    if age < 18:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "minors = titanic_survival.apply(is_minor, axis=1)\n",
    "print(minors.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    Adult\n",
      "1    Adult\n",
      "2    Adult\n",
      "3    Adult\n",
      "4    Adult\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "def generate_age_label(row):\n",
    "    age = row['Age']\n",
    "    if pd.isnull(age):\n",
    "        return 'Unknown'\n",
    "    elif age < 18:\n",
    "        return 'Minor'\n",
    "    else:\n",
    "        return 'Adult'\n",
    "    \n",
    "age_labels = titanic_survival.apply(generate_age_label, axis=1)\n",
    "print(age_labels.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            Survived\n",
      "age_labels          \n",
      "Adult       0.381032\n",
      "Minor       0.539823\n",
      "Unknown     0.293785\n"
     ]
    }
   ],
   "source": [
    "titanic_survival['age_labels'] = age_labels\n",
    "age_group_surval = titanic_survival.pivot_table(index='age_labels', values='Survived')\n",
    "print(age_group_surval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
